package gugu.framework.core;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import org.springframework.core.convert.converter.Converter;

public class StringLabelToCalendar implements Converter<String,Calendar>{
	private static final String DATE_FORMAT = "MM/dd/yyyy HH:mm:ss z";// 1-24hours
	private static TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");
	private static final SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
	@Override
	public Calendar convert(String arg0) {
		String originTz=arg0.split("#")[1];
		GregorianCalendar cal = new GregorianCalendar(utcTimeZone);
		Date date;
		try {
			date = formatter.parse(arg0.split("#")[0]);
			cal.setTime(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		cal.setTimeZone(TimeZone.getTimeZone(originTz));
		return cal;
	}
	
	public static void main(String[] args){
		String input="04/07/2012 03:49:45 UTC#GMT+8:00";
		String originTz=input.split("#")[1];
		GregorianCalendar cal = new GregorianCalendar(utcTimeZone);
		Date date;
		try {
			date = formatter.parse(input.split("#")[0]); 
			cal.setTime(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		cal.setTimeZone(TimeZone.getTimeZone(originTz));
		
		formatter.setCalendar(cal);
		System.out.print(formatter.format(cal.getTime()));
	}

}
